<template>
    <div>
        <!-- 搜索栏 -->
        <div style="margin: 10px 0;">
            <el-input placeholder="请输入名称" style="width: 200px;margin-right: 5px;"
                      suffix-icon="el-icon-search" v-model="role.name"></el-input>
            <el-button type="primary" @click="getAll">搜索</el-button>
            <el-button type="primary" @click="clearQuery">清空</el-button>
        </div>
        <!--操作栏-->
        <div style="margin: 10px 0;" v-if="user.role==='ROLE_ADMIN'">
            <el-button type="primary" @click="handleCreate($event)" id="新增" >新增<i class="el-icon-circle-plus-outline"></i>
            </el-button>
            <el-button type="danger" @click="delBatch" style="margin-right: 10px">批量删除<i
                    class="el-icon-remove-outline"></i></el-button>
        </div>
        <!--表格-->
        <el-table :data="tableData" :border="true" height="400" stripe
                  :header-cell-style="{background:'#eee'}"
                  ref="multipleTable"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="70" fixed="left"></el-table-column>
            <el-table-column prop="name" label="课程名称" width="120"></el-table-column>
            <el-table-column prop="score" label="学分" width="120"></el-table-column>
            <el-table-column prop="times" label="课时"></el-table-column>
            <el-table-column prop="teacher" label="授课老师"></el-table-column>
            <el-table-column prop="state" label="启用">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.state"
                            active-color="#13ce66"
                            inactive-color="#ff4949" @change="changeEnable(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="selectCourse(scope.row.id)" >选课 <i class="el-icon-edit"></i></el-button>
                    <el-button size="mini" type="success" @click="handleEdit(scope.row)" v-if="user.role!='ROLE_STUDENT'">编辑 <i class="el-icon-edit"></i></el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)" v-if="user.role==='ROLE_ADMIN'">删除 <i class="el-icon-remove-outline"></i></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页控件 -->
        <div class="block">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <!--新增框-->
        <div>
            <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
                <el-form :model="formData">
                    <el-form-item label="课程名称" :label-width="formLabelWidth">
                        <el-input v-model="formData.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学分" :label-width="formLabelWidth">
                        <el-input v-model="formData.score" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="课时" :label-width="formLabelWidth">
                        <el-input v-model="formData.times" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="授课老师" :label-width="formLabelWidth">
                        <el-select clearable v-model="formData.teacherId" placeholder="请选择角色" style="width: 100%">
                            <el-option v-for="item in teachers" :label="item.nickname" :key="item.nickname" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleAdd">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <!--编辑框-->
        <div>
            <el-dialog title="编辑" :visible.sync="dialogFormVisibleEdit">
                <el-form :model="formData">
                    <el-form-item label="ID" :label-width="formLabelWidth">
                        <el-input v-model="formData.id" autocomplete="off" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="课程名称" :label-width="formLabelWidth">
                        <el-input v-model="formData.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学分" :label-width="formLabelWidth">
                        <el-input v-model="formData.score" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="课时" :label-width="formLabelWidth">
                        <el-input v-model="formData.times" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="授课老师" :label-width="formLabelWidth">
                        <el-select clearable v-model="formData.teacherId" placeholder="请选择角色" style="width: 100%">
                            <el-option v-for="item in teachers" :label="item.nickname" :key="item.nickname" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                    <el-button type="primary" @click="handleAdd">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import request from "../Api/request";

    export default {
        name: "CourseView",
        data() {
            return {
                dialogTitle:'',
                //分页数据
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 2,
                role: {
                    name: '',
                },
                formData: {},
                dialogFormVisible: false,
                dialogFormVisibleEdit: false,
                formLabelWidth: '80px',
                multipleSelection: [],
                user:localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{},
                teachers:[],
            }
        },
        created() {
            this.getAll();
        },
        methods: {
            //分页
            getAll() {
                this.request.get("/courses/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.role.name,
                    }
                }).then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                }).catch(res => {
                });

                this.request.get("/users/roles/ROLE_TEACHER").then(res=>{
                    this.teachers=res.data;
                });

            },
            //清楚查询消息
            clearQuery() {
                this.role.name = "",
                    this.getAll()
            },
            //弹出添加窗口
            handleCreate(e) {
                this.dialogTitle=e.currentTarget.id;
                this.dialogFormVisible = true;
                this.resetForm();
            },
            //重置表单
            resetForm() {
                this.formData = {};
            },
            //新增或修改
            handleAdd() {
                this.request.post("/courses", this.formData).then(res => {
                    if (res.flag) {
                        this.getAll();
                        this.dialogFormVisibleEdit = false;
                        this.dialogFormVisible = false;
                        this.$message.success("保存成功");
                    }
                }).catch(() => {
                    this.$message.error("保存失败");
                })
            },
            //删除
            handleDelete(row) {
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {//确认删除
                    this.request.delete("/courses/" + row.id).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {//取消删除
                    this.$message.info("取消操作");
                });
            },
            //先查询，再编辑
            handleEdit(row) {
                this.request.get("/courses/" + row.id).then(res => {
                    //if (res.data.flag && res.data.data != null)
                    if (res.flag) {
                        this.dialogFormVisibleEdit = true;
                        this.formData = row;
                    } else {
                        this.$message.error("数据同步失败，自动刷新");
                    }
                }).finally(() => {
                    this.getAll();
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;//对象数组
            },
            //删除选中
            delBatch() {
                let ids = this.multipleSelection.map(value => value.id);//转化为id数组
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {
                    this.request.post("/courses/del/batch", ids).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    })
                }).catch(() => {
                    this.$message.info("取消操作");
                })
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAll();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAll();
            },
            //启用
            changeEnable(course){
                this.request.post("/courses",course).then(res=>{
                    if (res.flag){
                        this.$message.success("操作成功");
                    }else {
                        this.$message.error("操作失败");
                    }
                })
            },
            //选课
            selectCourse(courseId){
                this.request.post("/courses/studentCourse/"+this.user.id+"/"+courseId).then(res=>{
                    if (res.flag) {
                        this.$message.success("选课成功");
                    } else {
                        this.$message.error(res.msg);
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>